package com.wd.home.framgent


import android.content.Intent
import android.os.Build
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.annotation.RequiresApi
import androidx.fragment.app.viewModels
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.bw.base.view.PicSelectDialog
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.luck.picture.lib.thread.PictureThreadUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener
import com.stx.xhb.androidx.XBanner
import com.wd.common.base.BaseFragment
import com.wd.common.bean.BanneBean
import com.wd.common.viewmodel.MyViewModel
import com.wd.home.R
import com.wd.home.activity.SearchActivity
import com.wd.home.adapter.ManyRecAdapter
import com.wd.home.adapter.RecOneAdapter
import com.wd.home.databinding.FragmentHomeBinding
import com.wd.home.viewmodel.FindViewModel

class HomeFragment() : BaseFragment<FragmentHomeBinding>() {
    private lateinit var photoDialog: PicSelectDialog
    val viewmodel: MyViewModel by viewModels()
    private var adapter:RecOneAdapter?=null
    private var apdate:ManyRecAdapter?=null
    private val findViewModel: FindViewModel by viewModels()
    override fun getViewBinding(
        inflater: LayoutInflater,
        container: ViewGroup?
    ): FragmentHomeBinding =FragmentHomeBinding.inflate(layoutInflater)

    override fun initData() {
        photoDialog = PicSelectDialog(context, requireActivity())
        photoDialog.setOnSelectListener(object :
            PicSelectDialog.onItemClickListener {
            @RequiresApi(Build.VERSION_CODES.O)
            override fun onFile(mPhoto: List<String?>) {
                PictureThreadUtils.runOnUiThread {
                    mPhoto[0]?.apply {
                        findViewModel.getLicenseByOCR(this,requireActivity().applicationContext)
                    }
                }
            }
        })

        binding.homeSou.setOnClickListener {
            startActivity(Intent(context, SearchActivity::class.java))
        }
        binding.homeimg.setOnClickListener {
            XXPermissions.with(this).permission(Permission.READ_MEDIA_IMAGES, Permission.CAMERA)
                .request { permissions, all ->
                    if (!all) {
                        ToastUtils.showLong("获取部分权限成功，但部分权限未正常授予")
                        return@request
                    }
                    photoDialog.show()
                }

        }
        //轮播图
        viewmodel.getBanner()
        viewmodel.banner.observe(this, Observer {
            //加载广告图片
            binding.homeBanner.setBannerData(R.layout.item_homebann,it.result);
            binding.homeBanner.loadImage(XBanner.XBannerAdapter { banner, model, view, position -> //1、此处使用的Glide加载图片，可自行替换自己项目中的图片加载框架
                //2、返回的图片路径为Object类型，你只需要强转成你传输的类型就行，切记不要胡乱强转！
                Glide.with(this@HomeFragment)
                    .load((model as BanneBean.Result).imageUrl)
                    .placeholder(R.mipmap.home_cartwo)
                    .into(view.findViewById(R.id.home_bannimg))
            })
        })
        val arr1: Array<Int> = arrayOf(R.mipmap.home_nav_ranking,R.mipmap.home_nav_circles,R.mipmap.home_nav_look,R.mipmap.home_nav_new,R.mipmap.home_nav_newest,R.mipmap.home_nav_look,R.mipmap.home_nav_valuation,R.mipmap.home_nav_fuel,R.mipmap.home_nav_circles,R.mipmap.home_nav_electric)
        //金刚区
        viewmodel.getHomeRecOne()
        viewmodel.recone.observe(this, Observer {
            adapter=context?.let { it1-> RecOneAdapter(it1,it.result,arr1) }
            binding.homeRecone.layoutManager=LinearLayoutManager(context,LinearLayoutManager.HORIZONTAL,false)
            binding.homeRecone.adapter=adapter
        })
        //多条目
        var pageee = 1
        viewmodel.ManyRec(pageee,10)
        viewmodel.manyrecmu.observe(this){
                apdate=context?.let {it1-> ManyRecAdapter(it1,it) }
                binding.manyrec.layoutManager= LinearLayoutManager(context)
                binding.manyrec.adapter=apdate
        }
        val refreshLayout = binding.refreshLayout as RefreshLayout

        refreshLayout.setOnRefreshListener(object : OnRefreshListener {
            override fun onRefresh(refreshlayout: RefreshLayout) {
                viewmodel.ManyRec(1,10)
                refreshlayout.finishRefresh(2000 /*,false*/) //传入false表示刷新失败
            }
        })
        refreshLayout.setOnLoadMoreListener { refreshlayout ->
            pageee++
            viewmodel.ManyRec(pageee,10)
            refreshlayout.finishLoadMore(2000 /*,false*/) //传入false表示加载失败
        }
    }
    override fun onDestroyView() {
        super.onDestroyView()
        binding.homeBanner.removeAllViews()
    }

}